package jt.classic.system.objectquery.impl;

import jt.classic.system.objectquery.ObjectQuery;
import jt.classic.system.objectquery.ObjectQueryItem;
import jt.classic.system.workflow.WObject;

import org.limp.basework.AbstractSimpleBean;
import org.limp.basework.Table;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * 对象查询接口实现类
 *
 * @author mido
 *
 */
public class ObjectQueryImpl extends AbstractSimpleBean implements ObjectQuery {
    private String id;
    private String jsurl;
    private String cssurl;
    private String desc;
    private String name;
    private WObject object;
    private String queryurl;
    private String resulturl;
    private String purview;
    private String purviewstring;
    private String purviewname;
    private String export;
    private int session;
    private String createstime;
    private String createetime;
    private List querylist;
    private List viewlist;
    private List restraintlist;
    private Map handleMap;
    private boolean showChk;
    private boolean defaultlist;

    public WObject iobject() {
        // TODO Auto-generated method stub
        return this.object;
    }

    public String id() {
        // TODO Auto-generated method stub
        return this.id;
    }

    public String iJsUrl() {
        return this.jsurl;
    }

    public String iCssUrl() {
        return this.cssurl;
    }

    public String idesc() {
        // TODO Auto-generated method stub
        return this.desc;
    }

    public String iname() {
        // TODO Auto-generated method stub
        return this.name;
    }

    public int isession() {
        return this.session;
    }
    
    public boolean ishowchk(){
    	return this.showChk;
    }

    public String icreatestime() {
        return this.createstime;
    }

    public String icreateetime() {
        return this.createetime;
    }

    public boolean ihandle(String tp) {
        boolean rnt = (this.handleMap != null) &&
            this.handleMap.containsKey(tp) &&
            this.handleMap.get(tp).equals("yes");

        return rnt;
    }

    public String ipurview() {
        return this.purview;
    }

    public String ipurviewname() {
        return this.purviewname;
    }

    public String ipurviewstring() {
        return this.purviewstring;
    }

    public String iqueryurl() {
        // TODO Auto-generated method stub
        return this.queryurl;
    }

    public String iresulturl() {
        // TODO Auto-generated method stub
        return this.resulturl;
    }

    public ObjectQueryItem queryitem(String name) {
        // TODO Auto-generated method stub
        ObjectQueryItem item = null;

        if ((this.querylist != null) && !this.querylist.isEmpty()) {
            for (int i = 0; i < this.querylist.size(); i++) {
                item = (ObjectQueryItem) this.querylist.get(i);
            }
        }

        return item;
    }

    public List iqueryitems() {
        // TODO Auto-generated method stub
        return this.querylist;
    }

    public Map iqueryitemMap() {
        Map temp = new HashMap();

        if ((this.querylist != null) && !this.querylist.isEmpty()) {
            for (int i = 0; i < this.querylist.size(); i++) {
                ObjectQueryItem item = (ObjectQueryItem) this.querylist.get(i);
                temp.put(item.icolumn().getName(), item);
            }
        }

        return temp;
    }

    public List iviewitems() {
        return this.viewlist;
    }

    public Map iviewitemMap() {
        Map temp = new HashMap();

        if ((this.viewlist != null) && !this.viewlist.isEmpty()) {
            for (int i = 0; i < this.viewlist.size(); i++) {
                ObjectQueryItem item = (ObjectQueryItem) this.viewlist.get(i);
                temp.put(item.ifieldName(), item);
            }
        }

        return temp;
    }

    public List irestraintitems() {
        return this.restraintlist;
    }

    public Map irestraintitemMap() {
        Map temp = new HashMap();

        if ((this.restraintlist != null) && !this.restraintlist.isEmpty()) {
            for (int i = 0; i < this.restraintlist.size(); i++) {
                ObjectQueryItem item = (ObjectQueryItem) this.restraintlist.get(i);
                temp.put(item.icolumn().getName(), item);
            }
        }

        return temp;
    }

    public List iqueryitems(String tableName) {
        // TODO Auto-generated method stub
        List temp = new ArrayList();

        if ((this.querylist != null) && !this.querylist.isEmpty()) {
            for (int i = 0; i < this.querylist.size(); i++) {
                ObjectQueryItem item = (ObjectQueryItem) this.querylist.get(i);

                if (item.icolumn().associateTable().getName().equals(tableName)) {
                    temp.add(item);
                }
            }
        }

        return temp;
    }

    public List iviewitems(String tableName) {
        //    	 TODO Auto-generated method stub
        List temp = new ArrayList();

        if ((this.viewlist != null) && !this.viewlist.isEmpty()) {
            for (int i = 0; i < this.viewlist.size(); i++) {
                ObjectQueryItem item = (ObjectQueryItem) this.viewlist.get(i);

                if (!item.ialien() && item.icolumn().associateTable().getName().equals(tableName)) {
                    temp.add(item);
                }
            }
        }

        return temp;
    }

    public List irestraintitems(String tableName) {
        List temp = new ArrayList();

        if ((this.restraintlist != null) && !this.restraintlist.isEmpty()) {
            for (int i = 0; i < this.restraintlist.size(); i++) {
                ObjectQueryItem item = (ObjectQueryItem) this.restraintlist.get(i);

                if (item.icolumn().associateTable().getName().equals(tableName)) {
                    temp.add(item);
                }
            }
        }

        return temp;
    }

    public boolean isInsignificant() {
        return ((this.querylist == null) || this.querylist.isEmpty()) &&
        ((this.viewlist == null) || this.viewlist.isEmpty()) &&
        ((this.restraintlist == null) || this.restraintlist.isEmpty());
    }
    
    public boolean idefaultlist(){
    	return this.defaultlist;
    }
    
    public String iexport() {
        return this.export;
    }

    public void reload(Map temp) {
        if (temp != null) {
            this.info = temp;
        }

        this.id = (String) this.info.get("id");
        this.jsurl = (String) this.info.get("jsurl");
        this.cssurl = (String) this.info.get("cssurl");
        this.name = (String) this.info.get("name");
        this.desc = (String) this.info.get("desc");
        this.object = (WObject) this.info.get("object");
        this.queryurl = (String) this.info.get("queryurl");
        this.resulturl = (String) this.info.get("resulturl");
        this.purview = (String) this.info.get("purview");
        this.export = (String) this.info.get("export");
        this.purviewname = (String) this.info.get("purviewname");
        this.purviewstring = (String) this.info.get("purviewstring");
        this.createstime = (String) this.info.get("createstime");
        this.createetime = (String) this.info.get("createetime");
        this.session = this.info.containsKey("session")
            ? Integer.parseInt((String) this.info.get("session")) : 0;
        this.showChk =  "1".equals(this.info.get("showchk"));   
        this.handleMap = (Map) this.info.get("handleMap");
        this.querylist = (List) this.info.get("querylist");
        this.viewlist = (List) this.info.get("viewlist");
        this.restraintlist = (List) this.info.get("restraintlist");
        this.defaultlist = !"-1".equals(this.info.get("defaultlist"));
    }
}
